package com.woniu.secondhand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.entity.ResultCode;
import com.woniu.common.exception.LifeException;
import com.woniu.secondhand.constant.SecondhandProductConstant;
import com.woniu.secondhand.entity.vo.SellCollectVO;
import com.woniu.secondhand.mapper.SecondhandAccessMapper;
import com.woniu.secondhand.mapper.SecondhandCollectMapper;
import com.woniu.secondhand.mapper.SecondhandProductMapper;
import com.woniu.secondhand.pojo.SecondhandAccess;
import com.woniu.secondhand.pojo.SecondhandCollect;
import com.woniu.secondhand.pojo.SecondhandProduct;
import com.woniu.secondhand.service.ISecondhandCollectService;
import com.woniu.user.vo.UserLoginVO;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author HMMT
 * @since 2023-05-20
 */
@Service
public class SecondhandCollectServiceImpl extends ServiceImpl<SecondhandCollectMapper, SecondhandCollect> implements ISecondhandCollectService {

    @Autowired
    private SecondhandCollectMapper collectMapper;
    @Autowired
    private SecondhandProductMapper productMapper;
    @Autowired
    private SecondhandAccessMapper accessMapper;

    @Override
    public List<SellCollectVO> searchSell(UserLoginVO userLoginVO) {
        return getSellCollectVOList(userLoginVO, SecondhandProductConstant.SELL);
    }

    @Override
    public SecondhandCollect add(Integer id, UserLoginVO userLoginVO) {
        QueryWrapper<SecondhandCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userLoginVO.getId());
        wrapper.eq("product_id", id);
        SecondhandCollect selectCollect = collectMapper.selectOne(wrapper);
        if (selectCollect == null) {
            SecondhandCollect collect = new SecondhandCollect();
            collect.setUserId(userLoginVO.getId());
            collect.setProductId(id);
            SecondhandProduct product = productMapper.selectById(id);
            collect.setCollectPrice(product.getPrice());
            collectMapper.insert(collect);
            return collect;
        } else {
            throw new LifeException(ResultCode.DATA_ALREADY_EXISTED.getMessage(), ResultCode.DATA_ALREADY_EXISTED);
        }
    }

    @Override
    public List<SellCollectVO> searchWant(UserLoginVO userLoginVO) {
        return getSellCollectVOList(userLoginVO, SecondhandProductConstant.WANT);
    }

    @NotNull
    private List<SellCollectVO> getSellCollectVOList(UserLoginVO userLoginVO, Byte type) {
        LambdaQueryWrapper<SecondhandCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SecondhandCollect::getUserId, userLoginVO.getId());
        List<SecondhandCollect> secondhandCollects = collectMapper.selectList(wrapper);
        List<SellCollectVO> sellCollectVOList = new ArrayList<>();
        SellCollectVO sellCollectVO = new SellCollectVO();
        for (SecondhandCollect collect : secondhandCollects) {
            SecondhandProduct product = productMapper.selectById(collect.getProductId());
            if (Objects.equals(product.getType(), type)) {
                QueryWrapper<SecondhandAccess> accessQueryWrapper = new QueryWrapper<>();
                accessQueryWrapper.eq("product_id", product.getId());
                Integer accessCount = Math.toIntExact(accessMapper.selectCount(accessQueryWrapper));
                sellCollectVO.setId(collect.getProductId());
                sellCollectVO.setTitle(product.getTitle());
                sellCollectVO.setPic(product.getPic());
                sellCollectVO.setPrice(product.getPrice());
                sellCollectVO.setCollectPrice(collect.getCollectPrice());
                sellCollectVO.setAccessCount(accessCount);
                sellCollectVOList.add(sellCollectVO);
                sellCollectVO = new SellCollectVO();
                accessQueryWrapper.clear();
            }
        }
        return sellCollectVOList;
    }
}
